//
//  TabBarViewController.m
//  AutoHome
//
//  Created by Flying meat on 9/11/15.
//  Copyright (c) 2015 CaoFeng. All rights reserved.
//

#import "TabBarViewController.h"
#import "RecommendViewController.h"
#import "ForumViewController.h"
#import "FindCarViewController.h"
#import "DiscoverViewController.h"
#import "MineViewController.h"

@interface TabBarViewController ()

@end

@implementation TabBarViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    [self tabBarSet];
}
-(void)tabBarSet
{
    self.tabBar.hidden=YES;
    self.tab.tag=5;
   self.tab=[[UIView alloc]initWithFrame:CGRectMake(0, self.view.bounds.size.height-49, self.view.frame.size.width, 49)];
   self.tab.backgroundColor=[UIColor whiteColor];
    [self.view addSubview:self.tab];
    
    NSArray *unselectImage=@[@"item01@2x",@"item02@2x",@"item03@2x",@"item04_music@2x",@"item05@2x"];
    NSArray *selectImage = @[@"item01_selected@2x",@"item02_selected@2x",@"item03_selected@2x",@"item04_selected_music@2x",@"item05_selected@2x"];

    for (int i=0; i<5; i++) {
        UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
        button.frame = CGRectMake(i*(self.view.frame.size.width/5), 0, self.view.frame.size.width/5, 49);
        [button setBackgroundImage:[UIImage imageNamed:unselectImage[i]] forState:UIControlStateNormal];
        [button setBackgroundImage:[UIImage imageNamed:selectImage[i]] forState:UIControlStateSelected];
        button.tag = 100+i;
        [button addTarget:self action:@selector(click:) forControlEvents:UIControlEventTouchUpInside];
        [self.tab addSubview:button];
        
        //设置第一个为默认选中
        if (i==0)
        {
            button.selected = YES;
        }

    }
    UIView *v = [[UIView alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 1)];
    v.backgroundColor = [UIColor lightGrayColor];
    [self.tab addSubview:v];
    
    RecommendViewController *recommend=[self.storyboard instantiateViewControllerWithIdentifier:@"RecommendViewController"];
    UINavigationController *nav1 = [[UINavigationController alloc]initWithRootViewController:recommend];
    
    ForumViewController *foru=[self.storyboard instantiateViewControllerWithIdentifier:@"ForumViewController"];
    
    FindCarViewController *findcar=[self.storyboard instantiateViewControllerWithIdentifier:@"FindCarViewController"];
    
    DiscoverViewController *dicover=[self.storyboard instantiateViewControllerWithIdentifier:@"DiscoverViewController"];
    MineViewController *mine=[self.storyboard instantiateViewControllerWithIdentifier:@"MineViewController"];
    
    self.viewControllers=@[nav1,foru,findcar,dicover,mine];
    
    
    
}
- (void)click:(UIButton *)button
{
    for (UIButton *button in self.tab.subviews)
    {
        //如果是按钮,则取反状态
        if ([button isKindOfClass:[UIButton class]])
        {
            //将所有按钮的选择状态去除
            button.selected = NO;
        }
    }
    
    button.selected = YES;
    
    //设置tabBarController的选中项
    self.selectedIndex = button.tag - 100;
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
